<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/29
 * Time: 10:37
 */
namespace Admin\Controller;
use Admin\Controller\CommonController;

class ContactController extends CommonController{
    public function index(){
        $arr = M("contact")->select();
        $this->assign("arr",$arr);
        $this->display();
    }
    public function add(){
        if(empty($_POST)) {
            $this->display();
        }else{
            $data=I("post.");
            $test=M("contact")->data($data)->add();
            if($test) {
                $this->success('操作成功！',__MODULE__.'/contact/index');
            } else {
                $this->error('操作失败！',__MODULE__.'/contact/index');
            }
        }

    }
  
    public function edit(){
        if(IS_POST) {
            $id= I("get.nid");
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/contact/index');
            }else{
                $data=I("post.");
                $test=M("contact")->data($data)->where("id={$id}")->save();
                if($test) {
                    $this->success('操作成功！',__MODULE__.'/contact/index');
                } else {
                    $this->display("Public:error");
                }

            }
        }else{
            $id=I("get.nid");
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/contact/index');
            }else{
                $arr=M("contact")->where("id={$id}")->select();
                $this->assign("arr",$arr);
                $this->display();

            }
        }
    }
  
    public function del(){
        $nid = intval($_GET['nid']);
        if(empty($nid)) {
            $this->error('异常操作！',__MODULE__.'/contact/index');
        }
        if(M("contact")->where("id = {$nid}")->delete() !== fasle) {
            $this->success('操作成功！',__MODULE__.'/contact/index');
        } else {
            $this->error('操作失败！',__MODULE__.'/contact/index');
        }
    }
    //空方法
    public function _empty(){
        $this->error("404");
    }

}